package cn.com.shopec.erp.base.commo;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.ServletOutputStream;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;

public class SupplierExcel {

	/** 日志. */
	private static Logger log = Logger.getLogger(SupplierExcel.class);
	/** 工作簿. */
	private HSSFWorkbook workbook = null;
	/** 工作表. */
	private HSSFSheet sheet = null;
	/** 工作行. */
	private HSSFRow row = null;
	/** 单元 格. */
	private HSSFCell cell = null;
	/** 最大SHEET数量. */
	private static final int SHEET_MAX_COUNT = 65535;
	
	/**
	 * 
	 * @MethodName createWrite
	 * @Description 输出表格
	 * @author gaoyaxiong
	 * @date 2013-8-15  14:27:58
	 * @param out
	 */
	public void createWrite(OutputStream out) {
		try {
			workbook.write(out);
		} catch (FileNotFoundException e) {
			log.error("DataExport createWrite FileNotFoundException:", e);
		} catch (IOException e) {
			log.error("DataExport createWrite IOException:", e);
		} finally {
			if(out != null) {
				try {
					out.flush();
					out.close();
				} catch (IOException e) {
					log.error("DataExport createWrite IOException:", e);
				}
			}
		}
	}
	
	/**
	 * 
	 * @MethodName createSheet
	 * @Description 创建表格
	 * @author gaoyaxiong
	 * @date 2013-8-15  14:27:58
	 * @param list
	 * @param outputStream
	 */
	public void createSheet(List<?> list,
			ServletOutputStream outputStream) {
		//默认为1
        int sheetNum = 1;
        int totalSize = list.size()+20;

        //计算要创建的sheet个数
        if(totalSize % SHEET_MAX_COUNT > 0) {
            sheetNum = list.size() / SHEET_MAX_COUNT + 1;
        } else {
            sheetNum = list.size() / SHEET_MAX_COUNT;
        }
        
        workbook = new HSSFWorkbook();
        /**循环工作表**/
        for(int i = 0; i < sheetNum ; i++) {
    		//i ----------------工作表数量
        	sheet = workbook.createSheet(("sheet"+i));
        	
        	createFirstRow();
        	createSecondRow();
        	createThirdRow();
            createTitle();
            
            /**循环工作表的第一行**/
            for(int j=i*SHEET_MAX_COUNT; j<(i+1)*SHEET_MAX_COUNT; j++) {
            	if(j>totalSize) {
            		break;
            	} else {
            		/**初始化行对象**/
	        		if(j<list.size()) {
	        			row = sheet.createRow(j + 5 - (i * SHEET_MAX_COUNT));
	        			Object[] obj = (Object[]) list.get(j);
	        			setRowValue(obj, j);
	        		} else {
	        			break;
	        		}
            	}
            }
            
    	}
        
        createWrite(outputStream);
	}
	
	
	
	/**
	 * 
	 * @MethodName setRowValue
	 * @Description 设置行参数
	 * @author gaoyaxiong
	 * @date 2013-8-15  14:27:58
	 * @param poExcelVo
	 * @param j
	 */
	private void setRowValue(Object[] obj, int j) {
		if (null != obj) {
			cell = row.createCell(0);
			cell.setCellValue(String.valueOf(j + 1));
			//结算周期
			String accountsTime = "";
			for (int i = 0; i < obj.length; i++) {
				cell = row.createCell(i+1);
				String value = obj[i] == null ? "" : String.valueOf(obj[i]);
				
				switch (i) {
				case 12:
					if(obj[i] == null)break;
					switch (Integer.valueOf(obj[i].toString())) {
					case 0:
						cell.setCellValue("正常");
						break;
					case 1:
						cell.setCellValue("失效");
						break;
					default:
						break;
					}
					break;
				case 13:
					if(obj[i] == null)break;
					switch (Integer.valueOf(obj[i].toString())) {
					case 1:
						cell.setCellValue("购销结算");
						if(obj[i+1] == null)break;
						switch (Integer.valueOf(obj[16].toString())) {
						case 1:
							accountsTime = "预付";
							break;
						case 2:
							accountsTime = "货到7天";
							break;
						case 3:
							accountsTime = "货到15天";
							break;
						case 4:
							accountsTime = "货到30天";
							break;
						case 5:
							accountsTime = "月结15天";
							break;
						case 6:
							accountsTime = "月结30天";
							break;
						default:
							break;
						}
						
						
						break;
					case 2:
						cell.setCellValue("代销结算");
						if(obj[i+1] == null)break;
						switch (Integer.valueOf(obj[16].toString())) {
						case 1:
							accountsTime = "月结15天";
							break;
						case 2:
							accountsTime = "月结30天";
							break;
						default:
							break;
						}
						break;
					default:
						break;
					}
					break;
				case 16:
					cell.setCellValue(accountsTime);
					break;
				case 17:
					if(obj[i] == null)break;
					switch (Integer.valueOf(obj[i].toString())) {
					case 1:
						cell.setCellValue("扣点");
						break;
					case 2:
						cell.setCellValue("底价");
						break;
					default:
						break;
					}
					break;
				default:
					cell.setCellValue(value);
					break;
				}
				
				
			}
		}
	}
	
	/**
	 * 
	 * @MethodName createTitle
	 * @Description 设置标题
	 * @author gaoyaxiong
	 * @date 2013-8-15  14:27:58
	 */
	private void createTitle() {
		HSSFCellStyle style = workbook.createCellStyle();
	    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
	    style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		
		row = sheet.createRow(4);
	    row.setHeight((short) 400);
	    
	    String[] title = new String[]
	    		{"序号","供应商编号","供应商名称","供应商简称","联系人",
	    		"固定电话","手机号码","电子邮箱","联系地址",
	    		"经营类别","合同开始日期","合同结束日期",
	    		"注册时间","是否失效","结算类型","品牌名称",
	    		"品牌编码","结算周期","结算方式","扣点比例(%)"};
	    
	    for (int i=0; i<title.length; i++) {
	    	cell = row.createCell(i);
	    	cell.setCellStyle(style);
	    	cell.setCellValue(title[i]);
	    }
	}
	

	
	/**
	 * 
	 * @MethodName createThirdRow
	 * @Description 设置第三行
	 * @author gaoyaxiong
	 * @date 2013-8-15  14:27:58
	 */
	private void createThirdRow() {
		CellRangeAddress cra = new CellRangeAddress(2, 2, 0, 19);
	    sheet.addMergedRegion(cra);
	    row = sheet.createRow(2);
	    cell = row.createCell(0);
	    HSSFCellStyle style = workbook.createCellStyle();
	    style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
	    cell.setCellStyle(style);
	    cell.setCellValue("文件编码:________");
	}
	
	/**
	 * 
	 * @MethodName createSecondRow
	 * @Description 设置第二行
	 * @author gaoyaxiong
	 * @date 2013-8-15  14:27:58
	 */
	private void createSecondRow() {
		CellRangeAddress cra = new CellRangeAddress(1, 1, 0, 19);
	    sheet.addMergedRegion(cra);
	    row = sheet.createRow(1);
	    cell = row.createCell(0);
	    HSSFCellStyle style = workbook.createCellStyle();
	    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
	    HSSFFont font = workbook.createFont();
	    font.setFontHeight((short) 250);
	    style.setFont(font);
	    cell.setCellStyle(style);
	    cell.setCellValue("供应商信息");
	}
	
	/**
	 * 
	 * @MethodName createFirstRow
	 * @Description 设置第一行
	 * @author gaoyaxiong
	 * @date 2013-8-15  14:27:58
	 */
	private void createFirstRow() {
	    CellRangeAddress cra = new CellRangeAddress(0, 0, 0, 19);
	    sheet.addMergedRegion(cra);
	    row = sheet.createRow(0);
	    cell = row.createCell(0);
	    HSSFCellStyle style = workbook.createCellStyle();
	    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
	    HSSFFont font = workbook.createFont();
	    font.setFontHeight(HSSFFont.BOLDWEIGHT_NORMAL);
	    style.setFont(font);
	    cell.setCellStyle(style);
	    cell.setCellValue("深圳报业集团电子商务有限公司");
	}
}
